<!doctype html>
<html>
<head>
  <meta charset='utf-8'>
  <title>Load &amp; Save (Ajax) - Handsontable</title>

  <!--
  Loading Handsontable (full distribution that includes all dependencies apart from jQuery)
  -->
  <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/handsontable.css">
  <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/pikaday/pikaday.css">
  <script data-jsfiddle="common" src="../dist/pikaday/pikaday.js"></script>
  <script data-jsfiddle="common" src="../dist/moment/moment.js"></script>
  <script data-jsfiddle="common" src="../dist/zeroclipboard/ZeroClipboard.js"></script>
  <script data-jsfiddle="common" src="../dist/handsontable.js"></script>

  <!--
  Loading demo dependencies. They are used here only to enhance the examples on this page
  -->
  <link rel="stylesheet" media="screen" href="css/samples.css?20140331">
  <script  data-jsfiddle="common" src="js/samples.js"></script>
  <script src="js/highlight/highlight.pack.js"></script>
  <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">

  <!--
  Facebook open graph. Don't copy this to your project :)
  -->
  <meta property="og:title" content="Load &amp; Save (Ajax)">
  <meta property="og:description"
        content="Use the onChange callback to track changes made in the table. In the example below, $.ajax is used to load and save grid data.">
  <meta property="og:url" content="http://handsontable.com/demo/ajax.html">
  <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  <meta property="og:image:type" content="image/png">
  <meta property="og:image:width" content="409">
  <meta property="og:image:height" content="164">
  <link rel="canonical" href="http://handsontable.com/demo/ajax.html">

  <!--
  Google Analytics for GitHub Page. Don't copy this to your project :)
  -->
  <script src="js/ga.js"></script>
</head>

<body>


<div class="wrapper">
  <div class="wrapper-row">
    <div id="global-menu-clone">
      <h1><a href="../index.html">Handsontable</a></h1>

    </div>

    <div id="container">
      <div class="columnLayout">

        <div class="rowLayout">
          <div class="descLayout">
            <div class="pad" data-jsfiddle="example1">
              <h2>Load &amp; Save (Ajax)</h2>

              <p>Use the
                <b>onChange</b> callback to track changes made in the table. In the example below, $.ajax is used to load
                and save grid data.
              </p>

              <p>Note: this is a mockup. Nothing is acually saved. You have to implement that part server-side.</p>

              <p>
                <button name="load" id="load">Load</button>
                <button name="save" id="save">Save</button>
                <label><input type="checkbox" name="autosave" id="autosave" checked="checked" autocomplete="off"> Autosave</label>
              </p>

              <pre id="example1console" class="console">Click "Load" to load data from server</pre>

              <div id="example1"></div>

              <p>
                <button name="dump" data-dump="#example1" data-instance="hot" title="Prints current data source to Firebug/Chrome Dev Tools">
                  Dump data to console
                </button>
              </p>
            </div>
          </div>

          <div class="codeLayout">
            <div class="pad">
              <div class="jsFiddle">
                <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
              </div>
              <script data-jsfiddle="example1">
                var
                  $ = function(id) {
                      return document.getElementById(id);
                  },
                  container = $('example1'),
                  exampleConsole = $('example1console'),
                  autosave = $('autosave'),
                  load = $('load'),
                  save = $('save'),
                  autosaveNotification,
                  hot;

                hot = new Handsontable(container, {
                  startRows: 8,
                  startCols: 6,
                  rowHeaders: true,
                  colHeaders: true,
                  minSpareRows: 1,
                  contextMenu: true,
                  afterChange: function (change, source) {
                    if (source === 'loadData') {
                      return; //don't save this change
                    }
                    if (!autosave.checked) {
                      return;
                    }
                    clearTimeout(autosaveNotification);
                    ajax('json/save.json', 'GET', JSON.stringify({data: change}), function (data) {
                      exampleConsole.innerText  = 'Autosaved (' + change.length + ' ' + 'cell' + (change.length > 1 ? 's' : '') + ')';
                      autosaveNotification = setTimeout(function() {
                        exampleConsole.innerText ='Changes will be autosaved';
                      }, 1000);
                    });
                  }
                });

                Handsontable.Dom.addEvent(load, 'click', function() {
                  ajax('json/load.json', 'GET', '', function(res) {
                    var data = JSON.parse(res.response);

                    hot.loadData(data.data);
                    exampleConsole.innerText = 'Data loaded';
                  });
                });

                Handsontable.Dom.addEvent(save, 'click', function() {
                  // save all cell's data
                  ajax('json/save.json', 'GET', JSON.stringify({data: hot.getData()}), function (res) {
                    var response = JSON.parse(res.response);

                    if (response.result === 'ok') {
                      exampleConsole.innerText = 'Data saved';
                    }
                    else {
                      exampleConsole.innerText = 'Save error';
                    }
                  });
                });

                Handsontable.Dom.addEvent(autosave, 'click', function() {
                  if (autosave.checked) {
                    exampleConsole.innerText = 'Changes will be autosaved';
                  }
                  else {
                    exampleConsole.innerText ='Changes will not be autosaved';
                  }
                });
              </script>
            </div>
          </div>
        </div>

        <div class="footer-text">
        </div>
      </div>

    </div>

  </div>
</div>

<div id="outside-links-wrapper"></div>

</body>
</html>
